//
//  MainViewController.m
//  Slide
//
//  Created by jarze on 15/7/21.
//  Copyright (c) 2015年 jarze. All rights reserved.
//

#import "MainViewController.h"
#import "TouchPropagatedScrollView.h"
#import "VideoView.h"
#import "AViewController.h"


#import "MBProgressHUD+MJ.h"
#import "HMVideoCell.h"
#import "HMVideo.h"
#import <MediaPlayer/MediaPlayer.h>





#define MENU_HEIGHT 36
#define MENU_BUTTON_WIDTH  W/5
#define MIN_MENU_FONT  13.f
#define MAX_MENU_FONT  18.f


#define RGBA(R/*红*/, G/*绿*/, B/*蓝*/, A/*透明*/) \
[UIColor colorWithRed:R/255.f green:G/255.f blue:B/255.f alpha:A]

#define isIos7      ([[[UIDevice currentDevice] systemVersion] floatValue])

#define StatusbarSize ((isIos7 >= 7 && __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1)?20.f:0.f)

#define W [[UIScreen mainScreen] bounds].size.width


@interface MainViewController ()<UIScrollViewDelegate,UITableViewDelegate, UITableViewDataSource>
//@property (nonatomic, strong) NSArray *videos;

{
//    NSArray *_videos;
//    UITableView *_tableView;
//
    VideoView *_videoView;
    UIView *_navView;
    UIView *_topNaviV;
    UIScrollView *_scrollV;
    
    TouchPropagatedScrollView *_navScrollV;
    
   // UIScrollView *_navScrollV;
    
    float _startPointX;
    UIView *_selectTabV;
}

///@property (nonatomic, strong)UIView *navView;

@end

@implementation MainViewController


- (void)viewWillAppear:(BOOL)animated
{
    /* { hide status bar } */
    [[self navigationController] setNavigationBarHidden:YES];
    
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self addNav];
    
    [self addTop];
 
    
    // Do any additional setup after loading the view.
}

- (void)addNav
{
    
    UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, W , 0.f)];
    
    if (isIos7 >= 7 && __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1)
    {
        statusBarView.frame = CGRectMake(statusBarView.frame.origin.x, statusBarView.frame.origin.y, statusBarView.frame.size.width, 20.f);
        statusBarView.backgroundColor = [UIColor clearColor];
        ((UIImageView *)statusBarView).backgroundColor = RGBA(212,25,38,1);
        [self.view addSubview:statusBarView];
    }
    
    
    _navView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, StatusbarSize, W, 44.f)];
    ((UIImageView *)_navView).backgroundColor = RGBA(212,25,38,1);
    [self.view insertSubview:_navView belowSubview:statusBarView];
    _navView.userInteractionEnabled = YES;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((_navView.frame.size.width - 200)/2, (_navView.frame.size.height - 40)/2, 200, 40)];
    [titleLabel setText:@"新闻列表"];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:22]];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [_navView addSubview:titleLabel];
    
    UIButton *lbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [lbtn setFrame:CGRectMake(10, 2, 40, 40)];
    [lbtn setTitle:@"左" forState:UIControlStateNormal];
    lbtn.layer.borderWidth = 1;
    lbtn.layer.borderColor = [UIColor whiteColor].CGColor;
    lbtn.layer.masksToBounds = YES;
    lbtn.layer.cornerRadius = 20;
    [lbtn addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    [_navView addSubview:lbtn];
    
    UIButton *rbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rbtn setFrame:CGRectMake(_navView.frame.size.width - 50, 2, 40, 40)];
    [rbtn setTitle:@"右" forState:UIControlStateNormal];
    rbtn.layer.borderWidth = 1;
    rbtn.layer.borderColor = [UIColor whiteColor].CGColor;
    rbtn.layer.masksToBounds = YES;
    rbtn.layer.cornerRadius = 20;
    [rbtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    [_navView addSubview:rbtn];
    

}

-(void)addTop
{
    _topNaviV = [[UIView alloc] initWithFrame:CGRectMake(0, _navView.frame.size.height + _navView.frame.origin.y, W, MENU_HEIGHT)];
    _topNaviV.backgroundColor = RGBA(236.f, 236.f, 236.f, 1);
    [self.view addSubview:_topNaviV];
    
    //  float btnW = 30;
    //    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [btn setFrame:CGRectMake(_topNaviV.frame.size.width - btnW, 0, btnW, MENU_HEIGHT)];
    //    [btn setBackgroundColor:[UIColor redColor]];
    //    [btn setTitle:@"+" forState:UIControlStateNormal];
    //    [_topNaviV addSubview:btn];
    //    [btn addTarget:self action:@selector(showSelectView:) forControlEvents:UIControlEventTouchUpInside];

    
    NSArray *arT = @[@"小黄人", @"国际", @"娱乐", @"体育", @"测试5", @"测试6", @"测试7", @"测试8"];
    
    
    _navScrollV = [[TouchPropagatedScrollView alloc] initWithFrame:CGRectMake(0, 0, W, MENU_HEIGHT)];
    
    [_navScrollV setShowsHorizontalScrollIndicator:NO];
    
    for (int i = 0; i < [arT count]; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(MENU_BUTTON_WIDTH * i, 0, MENU_BUTTON_WIDTH, MENU_HEIGHT)];
        [btn setTitle:[arT objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
                btn.tag = i + 1;
        
                if(i==0)
                {
                  //  [self changeColorForButton:btn red:1];
                    btn.titleLabel.font = [UIFont systemFontOfSize:MAX_MENU_FONT];
                    
                    [self changeColorForButton:btn red:1];

                  //  btn.backgroundColor = RGBA(150, 150, 150, 0.6);
                }else
                {
                    btn.titleLabel.font = [UIFont systemFontOfSize:MIN_MENU_FONT];
                  //  [self changeColorForButton:btn red:0];
                }
        
        
        [btn addTarget:self action:@selector(actionbtn:) forControlEvents:UIControlEventTouchUpInside];
        [_navScrollV addSubview:btn];
    }
    
    [_navScrollV setContentSize:CGSizeMake(MENU_BUTTON_WIDTH * [arT count], MENU_HEIGHT)];
    [_topNaviV addSubview:_navScrollV];
    
    
    [self addContentView];
    
    [self addView2Page:_scrollV count:[arT count]];
    

}


- (void)addContentView
{
    _scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _topNaviV.frame.origin.y + _topNaviV.frame.size.height, W, self.view.frame.size.height - _topNaviV.frame.origin.y -  _topNaviV.frame.size.height )];
    [_scrollV setPagingEnabled:YES];
    [_scrollV setShowsHorizontalScrollIndicator:NO];
    [self.view insertSubview:_scrollV aboveSubview:_navView];
    
    _scrollV.delegate = self;
    
    //拖动手势
    [_scrollV.panGestureRecognizer addTarget:self action:@selector(scrollHandlePan:)];
    
//    _selectTabV = [[UIView alloc] initWithFrame:CGRectMake(0, _scrollV.frame.origin.y - _scrollV.frame.size.height, _scrollV.frame.size.width, _scrollV.frame.size.height)];
//    [_selectTabV setBackgroundColor:RGBA(236.f, 236.f, 236.f, 1)];
//    [_selectTabV setHidden:YES];
//    [self.view insertSubview:_selectTabV belowSubview:_navView];
    
   
}

- (void)addView2Page:(UIScrollView *)scrollV count:(NSUInteger)pageCount
{
    
    VideoView *view = [[VideoView alloc] initWithFrame:CGRectMake(0, 0, scrollV.frame.size.width, scrollV.frame.size.height)];
    
    [scrollV addSubview:view];

    
    for (int i = 0; i < pageCount; i++)
    {
        if (i == 0) {
            _videoView = [[VideoView alloc] initWithFrame:CGRectMake(0, 0, scrollV.frame.size.width, scrollV.frame.size.height)];
            _videoView.delegate = self;
            _videoView.dataSource = self;
            _videoView.tag = i + 1;
            _videoView.userInteractionEnabled = YES;
            [scrollV addSubview:_videoView];

            
        }else{
        
        UIScrollView *pageScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(scrollV.frame.size.width * i, 0, scrollV.frame.size.width, scrollV.frame.size.height)];
        

        
//        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, scrollV.frame.size.width, scrollV.frame.size.height)];
//        _tableView.delegate = self;
//        _tableView.dataSource = self;
//        _tableView.tag = i + 1;
//        _tableView.userInteractionEnabled = YES;
       
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollV.frame.size.width, scrollV.frame.size.height)];
      
        view.tag = i + 1;
        view.userInteractionEnabled = YES;
            
            
        UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] init];
        singleTapRecognizer.numberOfTapsRequired = 1;
        [singleTapRecognizer addTarget:self action:@selector(pust2View:)];
        [view addGestureRecognizer:singleTapRecognizer];
        
        NSLog(@"%d-----", i);
        
         [self initPageView:view];

        [pageScroll addSubview:view];
        [scrollV addSubview:pageScroll];
        
        [pageScroll setContentSize:CGSizeMake(view.frame.size.width, 1000)];
        }

        
    }
    
    [scrollV setContentSize:CGSizeMake(scrollV.frame.size.width * pageCount, scrollV.frame.size.height)];
}


- (void)pust2View:(id)sender
{
    AViewController *aViewController = [[AViewController alloc] init];
    [self.navigationController pushViewController:aViewController animated:YES];
   }

- (void)addContentOfTableView
{
   
}




- (void)initPageView:(UIView *)view
{
//    
//    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 200)];
//    v.backgroundColor = [UIColor grayColor];
//    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 100, 200)];
//    
//    [l setBackgroundColor:[UIColor colorWithRed:(float)(1+arc4random()%99)/100 green:(float)(1+arc4random()%99)/100 blue:(float)(1+arc4random()%99)/100 alpha:1]];
//    [v addSubview:l];
//   
//    
//    [view addSubview:v];
    
    int width = (view.frame.size.width - 20)/3;
    float x = 5;
    float y = 4;
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height - 64)];
    int sumJ = (int)(1+arc4random()%4);
    int sumI = (int)(1+arc4random()%3);
    for (int j = 1; j <= sumJ; j++)
    {
        for (int i = 1; i <= 3; i++)
        {
            if (j == sumJ && i > sumI)
            {
                break;
            }
            float w = x * i + width * (i - 1);
            float h = y * j + width * (j - 1);
            UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(w, h, width, width)];
            [l setBackgroundColor:[UIColor colorWithRed:(float)(1+arc4random()%99)/100 green:(float)(1+arc4random()%99)/100 blue:(float)(1+arc4random()%99)/100 alpha:1]];
            [v addSubview:l];
        }
    }
    
    [view addSubview:v];
}

//

- (void)scrollHandlePan:(UIPanGestureRecognizer *)panParam

{
    BOOL isPaning = NO;
    if(_scrollV.contentOffset.x < 0)
    {
        isPaning = YES;
    }
    else if(_scrollV.contentOffset.x > (_scrollV.contentSize.width - _scrollV.frame.size.width))
    {
        isPaning = YES;

    }
    if (isPaning) {
        
       // [AViewController sharedSliderController]
    }



}

- (void)actionbtn:(UIButton *)btn
{
    
    [_scrollV scrollRectToVisible:CGRectMake(_scrollV.frame.size.width * (btn.tag - 1), _scrollV.frame.origin.y, _scrollV.frame.size.width, _scrollV.frame.size.height) animated:YES];
    
    float xx = _scrollV.frame.size.width * (btn.tag - 1) * (MENU_BUTTON_WIDTH / self.view.frame.size.width) - MENU_BUTTON_WIDTH;
    [_navScrollV scrollRectToVisible:CGRectMake(xx, 0, _navScrollV.frame.size.width, _navScrollV.frame.size.height) animated:YES];

}



- (void)changeView:(float)x
{
    float xx = x * (MENU_BUTTON_WIDTH / self.view.frame.size.width);
    
    float startX = xx;

    
    int sT = x/_scrollV.frame.size.width + 1;
    
    if (sT <= 0)
    {
        return;
    }
    
    UIButton *btn = (UIButton *)[_navScrollV viewWithTag:sT];
    float percent = (startX - MENU_BUTTON_WIDTH * (sT - 1))/MENU_BUTTON_WIDTH;
    float value = MIN_MENU_FONT + (1 - percent) * (MAX_MENU_FONT - MIN_MENU_FONT);
    btn.titleLabel.font = [UIFont systemFontOfSize:value];
    [self changeColorForButton:btn red:(1 - percent)];
    
    UIButton *btn2 = (UIButton *)[_navScrollV viewWithTag:sT + 1];
    float value2 = MIN_MENU_FONT + percent * (MAX_MENU_FONT - MIN_MENU_FONT);
    btn2.titleLabel.font = [UIFont systemFontOfSize:value2];
    [self changeColorForButton:btn2 red:percent];

    
    if (sT >=2) {
        UIButton *btn3 = (UIButton *)[_navScrollV viewWithTag:sT - 1];
        btn3.titleLabel.font = [UIFont systemFontOfSize:value2];
        [self changeColorForButton:btn3 red:percent];
  
    }
   
}

- (void)changeColorForButton:(UIButton *)btn red:(float)nRedPercent
{
    float value = nRedPercent * 212;
    [btn setTitleColor:RGBA(value,25,38,1) forState:UIControlStateNormal];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
 
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self changeView:scrollView.contentOffset.x];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    float xx = scrollView.contentOffset.x * (MENU_BUTTON_WIDTH / W) - MENU_BUTTON_WIDTH;
    
    [_navScrollV scrollRectToVisible:CGRectMake(xx, 0, _navScrollV.frame.size.width, _navScrollV.frame.size.height) animated:YES];


}

- (void)leftAction:(UIButton *)btn
{

}

- (void)rightAction:(UIButton *)btn
{



}




// videoView的内容设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _videoView.videos.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMVideoCell *cell = [HMVideoCell cellWithTableView:tableView];
    
    cell.video = _videoView.videos[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMVideo *video = _videoView.videos[indexPath.row];
    
    // 播放视频
    NSString *videoUrl = [NSString stringWithFormat:@"http://192.168.1.18:8080/MJServer/%@", video.url];
    
    //   NSString *videoUrl = [[NSBundle mainBundle] pathForResource:@"minion_01.mp4" ofType:nil];
    
    MPMoviePlayerViewController *playerVc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:videoUrl]];
    
    [self presentMoviePlayerViewControllerAnimated:playerVc];
    
    //    MPMoviePlayerController; // 可以随意控制播放器的尺寸
}

@end
